package MST;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class MST1705 {
    public String[] findLongestSubarray(String[] array) {
        int maxLen=0;
        int left=0,right=0;
        int len = array.length;
        int diff = 0;
        Map<Integer,Integer> diffs=new HashMap<>();
        diffs.put(0,0);
        for (int i = 0; i < len; i++) {
            if (Character.isDigit(array[i].charAt(0))){
                diff++;
            }else {
                diff--;
            }
            if (diffs.containsKey(diff)){
                int t=diffs.get(diff);
                if (i-t>maxLen){
                    maxLen=i-t;
                    left=t;
                    right=i+1;
//                    System.out.println(left+"  "+right+"  "+maxLen+"  "+diff);
                }
            }else {
                diffs.put(diff,i+1);
            }
        }
        String[] re= Arrays.copyOfRange(array,left,right);
        return re;
    }
    @Test
    public void test() {
        String []s={"A","1","B","C","D","2","3","4","E","5","F","G","6","7","H","I","J","K","L","M"};
//        String []s={"A","1"};
        String[] re=findLongestSubarray(s);
        for (String r:re){
            System.out.print(r);
        }

    }
}
